﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Axis.Dao.Core;
using Axis.BusinessObject.Response;
using Axis.BusinessObject.Core;
using Axis.Web.Administrator.Global;
namespace Axis.Web.Administrator.Views.Login
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CSecurity.USER_ID = 0;
                CSecurity.SECRECT_TOKEN = null;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                CCoreDao core = new CCoreDao();
                string userName = txtLoginName.Text.Trim();
                string password = txtPassword.Text.Trim();
                string inputValue = string.Format("<InputValue LoginName='{0}' Password='{1}' />", userName, password);
                CLoginResponse response = core.Login(inputValue);
                if (response.IsSuccessfull)
                {
                    CUserList user = response.UserList;
                    CSecurity.USER_ID = user.ID;
                    Session["USERID"] = user.ID;
                    CSecurity.USER_NAME = user.LoginName;
                    Session["USERNAME"] = user.LoginName;
                    CSecurity.USER_FIRSTNAME = user.FullName;
                    CSecurity.USER_TYPE = Convert.ToInt32(user.Type);
                    if (user.Type == 2)
                    {
                        CSecurity.IS_SUPER_ADMIN = true;
                        Session["ISSUPERADMIN"] = true;
                    }
                    else if (user.Type == 999)
                    {
                        CSecurity.IS_HOST_ADMIN = true;
                        Session["ISHOSTADMIN"] = true;
                    }
                    CSecurity.IS_ADMIN = true;
                    if (!string.IsNullOrEmpty(user.SecrectToken))
                    {
                        CSecurity.SECRECT_TOKEN = user.SecrectToken;
                    }
                    Response.Redirect("~/Administrator/Views/Home/Default.aspx");
                }
                else
                {
                    pnlLoginError.Visible = true;
                    ltrLoginError.Text = "Login Failed. Please remember that passwords are case sensitive.";
                }
            }
            catch (Exception ex) { 
                
            }
        }
    }
}